﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel.DomainServices.Client;

using System.Collections.ObjectModel;

using LEEAAttendanceControl.General;

namespace LEEAAttendanceControl.Views
{
    public partial class DepartmentEdit : ChildWindow
    {
        LEEAContext dc = new LEEAContext();

        public Department currdep = new Department();
        
        public DepartmentEdit()
        {
            InitializeComponent();
        }

        public DepartmentEdit(string title)
        {
            Title = title;

            InitializeComponent();
        }

        public void LoadData(int Id)
        {
            dc.Load(dc.GetCompaniesQuery()).Completed += (s, e) =>
                {
                    comboCompanies.ItemsSource = dc.Companies;

                    dc.Load(dc.GetDepartmentByDepartmentIdQuery(Id)).Completed += (s1, e1) =>
                    {
                        currdep = (Department)dc.Departments.FirstOrDefault();

                        if (currdep == null)
                        {
                            currdep = new Department();

                            dc.GetDepartmentMaxCode(code =>
                            {
                                txtDepartmentCode.Text = code.Value!=null?code.Value.Value.ToString():No.One.ToString();
                            }, null);

                            dc.Departments.Add(currdep);
                        }

                        GRDDepartment.DataContext = currdep;

                        comboCompanies.SelectedValue = currdep.CompanyID;
                    };
                };
       }

       private void OnDepartmenLoaded(LoadOperation lo)
       {
           if (lo.HasError)
           {
               lo.MarkErrorAsHandled();
               MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralError
                   , ApplicationStrings.GeneralError, "Error");

               message.Show();
           }
           else
           {
               currdep = (Department)lo.Entities.FirstOrDefault();

               if (currdep == null)
               {
                   currdep = new Department();
                   dc.Departments.Add(currdep);
               }

               GRDDepartment.DataContext = currdep;
           }
        }

        private void OnChangesSubmitted(SubmitOperation so)
        {
            if (so.HasError)
            {
                so.MarkErrorAsHandled();
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralError
                    , ApplicationStrings.GeneralError, "Error");
                message.Show();
            }
            else
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralSave
                    , ApplicationStrings.GeneralSave, "Save");

                message.Closed += (s, e) =>
                {
                    this.DialogResult = true;
                };

                message.Show();
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            dc.SubmitChanges(OnChangesSubmitted, null);
        }
    }
}

